package com.sebiotec.driver.ui.activity;

import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.lifecycle.ViewModelProviders;

import com.allen.library.RxHttpUtils;
import com.sebiotec.basecommon.utils.base.BaseActivity;
import com.sebiotec.basecommon.utils.entity.CardTypeUtils;
import com.sebiotec.basecommon.utils.tools.ActivityTool;
import com.sebiotec.basecommon.utils.tools.TimeUtil;
import com.sebiotec.driver.R;
import com.sebiotec.driver.R2;
import com.sebiotec.driver.model.event.JobEvent;
import com.sebiotec.driver.model.response.JobDetailResponse;
import com.sebiotec.driver.viewmodel.JobDetailViewModel;
import com.hedan.textdrawablelibrary.TextViewDrawable;
import com.xuexiang.xui.widget.button.ButtonView;
import com.xuexiang.xui.widget.dialog.materialdialog.DialogAction;
import com.xuexiang.xui.widget.dialog.materialdialog.GravityEnum;
import com.xuexiang.xui.widget.dialog.materialdialog.MaterialDialog;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.OnClick;

public class JobDetailActivity extends BaseActivity {

    @BindView(R2.id.iv_back)
    ImageView ivBack;
    @BindView(R2.id.textView8)
    TextView textView8;
    @BindView(R2.id.tv_message_passenger)
    TextView tvMessagePassenger;
    @BindView(R2.id.tv_pax_lug)
    TextView tvPaxLug;
    @BindView(R2.id.tv_message_detail_clientid)
    TextView tvMessageDetailClientid;
    @BindView(R2.id.tv_pick_up_date)
    TextView tvPickUpDate;
    @BindView(R2.id.tv_pick_up_from)
    TextViewDrawable tvPickUpFrom;
    @BindView(R2.id.tv_itinerary)
    TextView tvItinerary;
    @BindView(R2.id.tv_signboard)
    TextView tvSignboard;
    @BindView(R2.id.tv_remake)
    TextView tvRemake;
    @BindView(R2.id.tv__type_vehice)
    TextView tv__type_vehice;
    @BindView(R2.id.tv_book_by)
    TextView tv_book_by;
    @BindView(R2.id.tv_plate)
    TextView tv_plate;
    @BindView(R2.id.tv_destination)
    TextViewDrawable tvDestination;
    @BindView(R2.id.tv_pick_up_time)
    TextView tvPickUpTime;
    @BindView(R2.id.btn_pick_up)
    ButtonView btnPickUp;
    @BindView(R2.id.btn_drop)
    ButtonView btnDrop;
    @BindView(R2.id.btn_compete)
    ButtonView btnCompete;

    @BindView(R2.id.tv_card_type)
    TextView tv_card_type;
    @BindView(R2.id.tv_pay_id)
    TextView tv_pay_id;
    @BindView(R2.id.cb_round_trip)
    CheckBox cb_round_trip;
    private int status = 0;
    private int pos = 0;
    private  String BookId;
    private JobDetailViewModel jobDetailViewModel;
    private int readStatus;

    @Override
    protected int setLayoutId() {
        return R.layout.activity_job_detail;
    }

    @Override
    protected void initUI() {
        pos = getIntent().getIntExtra("pos", 0);
        status = getIntent().getIntExtra("status", 0);
        readStatus = getIntent().getIntExtra("readStatus", 0);
        BookId  = getIntent().getStringExtra("BookId");

         jobDetailViewModel = ViewModelProviders.of(this).get(JobDetailViewModel.class);
        if (status == 7||status==2) {
            btnDrop.setStrokeColorAndWidth(1,getColor(R.color.gray));
            btnDrop.setSolidColor(getColor(R.color.gray01));
            btnDrop.setTextColor(getColor(R.color.white));
            btnDrop.setEnabled(false);
            btnCompete.setEnabled(false);
            btnCompete.setStrokeColorAndWidth(1,getColor(R.color.gray));
            btnCompete.setSolidColor(getColor(R.color.gray01));
            btnCompete.setTextColor(getColor(R.color.white));



        }else if (status==3){

            btnPickUp.setStrokeColorAndWidth(1,getColor(R.color.gray));
            btnPickUp.setSolidColor(getColor(R.color.gray01));
            btnPickUp.setTextColor(getColor(R.color.white));

            btnPickUp.setEnabled(false);
            btnCompete.setEnabled(false);
            btnCompete.setStrokeColorAndWidth(1,getColor(R.color.gray));
            btnCompete.setSolidColor(getColor(R.color.gray01));
            btnCompete.setTextColor(getColor(R.color.white));


        }else if (status==4){
            btnPickUp.setStrokeColorAndWidth(1,getColor(R.color.gray));
            btnPickUp.setSolidColor(getColor(R.color.gray01));
            btnPickUp.setTextColor(getColor(R.color.white));

            btnPickUp.setEnabled(false);
            btnDrop.setEnabled(false);
            btnDrop.setStrokeColorAndWidth(1,getColor(R.color.gray));
            btnDrop.setSolidColor(getColor(R.color.gray01));
            btnDrop.setTextColor(getColor(R.color.white));


        }else {
            btnPickUp.setVisibility(View.GONE);
            btnDrop.setVisibility(View.GONE);
            btnCompete.setVisibility(View.GONE);

        }


    }

    @Override
    protected void initData() {
        if (readStatus==0){
            jobDetailViewModel.isRead(BookId,1);
        }


        jobDetailViewModel.getData(BookId, new JobDetailViewModel.MyJobDetailDataCallBack<Object>() {
            @Override
            public void onDataSuccessCallBack(Object o) {
                JobDetailResponse booking = (JobDetailResponse) o;
                tvMessagePassenger.setText(booking.getPassengers()+"");
                tvPaxLug.setText(booking.getLug()+"");
                tvMessageDetailClientid.setText(booking.getClientId());
                tvPickUpDate.setText(TimeUtil.getFormatData_ns(booking.getPickupDateTime()));
                tvPickUpTime.setText(TimeUtil.getFormatData_m(booking.getPickupDateTime()));
                tvPickUpFrom.setText(booking.getPickupFrom());
                tvDestination.setText(booking.getDestination());
                textView8.setText(booking.getName());
                tvSignboard.setText(booking.getSignboard());
                tv__type_vehice.setText(booking.getVechileTypeDescr());
                tv_plate.setText(booking.getVehPlate());
                tv_book_by.setText(booking.getCreateUser());
                tvRemake.setText(booking.getRemark());
                tvItinerary.setText(booking.getItinerary());
                tv_card_type.setText(CardTypeUtils.Format(booking.getCardType()));
                tv_pay_id.setText(booking.getPaymentID());
                if (booking.getRoundTrip()==1){
                    cb_round_trip.setChecked(true);
                    cb_round_trip.setClickable(false);
                }else{
                    cb_round_trip.setChecked(false);
                    cb_round_trip.setClickable(false);
                }

            }

            @Override
            public void onDataFailCallBack(Object o) {

            }
        });
    }

    @Override
    protected void setListener() {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        RxHttpUtils.cancel("JobDetail");
    }

    @OnClick({R2.id.iv_back, R2.id.btn_pick_up, R2.id.btn_drop, R2.id.btn_compete})
    public void onViewClicked(View view) {
        int id = view.getId();
        if (id == R.id.iv_back) {
            ActivityTool.getInstance().popOneActivity(this);
        } else if (id == R.id.btn_pick_up) {
            new MaterialDialog.Builder(this)
                    .content(getString(R.string.job_picked_up))
                    .contentGravity(GravityEnum.CENTER)
                    .negativeColor(getResources().getColor(R.color.smj))
                    .positiveColor(getResources().getColor(R.color.smj))
                    .buttonsGravity(GravityEnum.CENTER)
                    .positiveText(getString(R.string.yes))
                    .negativeText(getString(R.string.no_login))
                    .onPositive(new MaterialDialog.SingleButtonCallback() {
                        @Override
                        public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
//                            ActivityTool.getInstance().finishAllActivities();
//                          ARouter.getInstance().build("/Login/LoginActivity").navigation(getContext().getApplicationContext());

                            //发消息让Man关闭
                            jobDetailViewModel.update(BookId, 3, new JobDetailViewModel.MyJobDetailDataCallBack<Object>() {
                                @Override
                                public void onDataSuccessCallBack(Object o) {
                                    btnPickUp.setStrokeColorAndWidth(1,getColor(R.color.gray));
                                    btnPickUp.setSolidColor(getColor(R.color.gray01));
                                    btnPickUp.setTextColor(getColor(R.color.white));
                                    btnPickUp.setEnabled(false);
                                    btnCompete.setEnabled(false);
                                    btnCompete.setStrokeColorAndWidth(1,getColor(R.color.gray));
                                    btnCompete.setSolidColor(getColor(R.color.gray01));
                                    btnCompete.setTextColor(getColor(R.color.white));
                                    btnDrop.setEnabled(true);
                                    btnDrop.setStrokeColorAndWidth(1,getColor(R.color.smj));
                                    btnDrop.setSolidColor(getColor(R.color.white));
                                    btnDrop.setTextColor(getColor(R.color.smj));

                                    EventBus.getDefault().post(new JobEvent(1));
                                }

                                @Override
                                public void onDataFailCallBack(Object o) {

                                }
                            });
                        }
                    }).show();




        } else if (id == R.id.btn_drop) {
            new MaterialDialog.Builder(this)
                    .content(getString(R.string.job_droped_off))
                    .contentGravity(GravityEnum.CENTER)
                    .negativeColor(getResources().getColor(R.color.smj))
                    .positiveColor(getResources().getColor(R.color.smj))
                    .buttonsGravity(GravityEnum.CENTER)
                    .positiveText(getString(R.string.yes))
                    .negativeText(getString(R.string.no_login))
                    .onPositive(new MaterialDialog.SingleButtonCallback() {
                        @Override
                        public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
//                            ActivityTool.getInstance().finishAllActivities();
//                          ARouter.getInstance().build("/Login/LoginActivity").navigation(getContext().getApplicationContext());

                            jobDetailViewModel.update(BookId, 4, new JobDetailViewModel.MyJobDetailDataCallBack<Object>() {
                                @Override
                                public void onDataSuccessCallBack(Object o) {
                                    btnDrop.setEnabled(false);
                                    btnDrop.setStrokeColorAndWidth(1,getColor(R.color.gray));
                                    btnDrop.setSolidColor(getColor(R.color.gray01));
                                    btnDrop.setTextColor(getColor(R.color.white));

                                    btnCompete.setEnabled(true);
                                    btnCompete.setStrokeColorAndWidth(1,getColor(R.color.smj));
                                    btnCompete.setSolidColor(getColor(R.color.white));
                                    btnCompete.setTextColor(getColor(R.color.smj));
                                    EventBus.getDefault().post(new JobEvent(1));
                                }

                                @Override
                                public void onDataFailCallBack(Object o) {

                                }
                            });
                        }
                    }).show();




        } else if (id == R.id.btn_compete) {
            new MaterialDialog.Builder(this)
                    .content(getString(R.string.job_completed))
                    .contentGravity(GravityEnum.CENTER)
                    .negativeColor(getResources().getColor(R.color.smj))
                    .positiveColor(getResources().getColor(R.color.smj))
                    .buttonsGravity(GravityEnum.CENTER)
                    .positiveText(getString(R.string.yes))
                    .negativeText(getString(R.string.no_login))
                    .onPositive(new MaterialDialog.SingleButtonCallback() {
                        @Override
                        public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
//                            ActivityTool.getInstance().finishAllActivities();
//                          ARouter.getInstance().build("/Login/LoginActivity").navigation(getContext().getApplicationContext());

                            //发消息让Man关闭
                            jobDetailViewModel.update(BookId, 5, new JobDetailViewModel.MyJobDetailDataCallBack<Object>() {
                                @Override
                                public void onDataSuccessCallBack(Object o) {

                                    btnCompete.setEnabled(false);
                                    btnCompete.setStrokeColorAndWidth(1,getColor(R.color.gray));
                                    btnCompete.setSolidColor(getColor(R.color.gray01));
                                    btnCompete.setTextColor(getColor(R.color.white));
                                    EventBus.getDefault().post(new JobEvent(2,pos));
                                    ActivityTool.getInstance().popOneActivity(JobDetailActivity.this);
                                }

                                @Override
                                public void onDataFailCallBack(Object o) {

                                }
                            });
                        }
                    }).show();


        }
    }
}
